<template>
  <a-card :bordered="false">
    <a-table
      ref="table"
      :row-key="
        (record) => {
          return record.userAnswerId
        }
      "
      :columns="columns"
      :data-source="data"
      :loading="loading"
      :pagination="pagination"
      @change="onChange"
    >
      <span slot="areaName" slot-scope="text, record">
        {{ record.provinceName }}{{ record.cityName }}{{ record.areaName }}
      </span>
    </a-table>
  </a-card>
</template>

<script>
const columns = [
  {
    title: '序号',
    dataIndex: 'userAnswerId'
  },
  {
    title: '题目标题',
    dataIndex: 'questionTitle'
  },
  {
    title: '回答',
    dataIndex: 'answer'
  },
  {
    title: '是否正确',
    dataIndex: 'isRightText'
  },
  {
    title: '获得积分',
    dataIndex: 'credit'
  },
  {
    title: '创建时间',
    dataIndex: 'createdAt'
  }
]

export default {
  name: 'AnswerList',
  data() {
    return {
      url: '',
      columns,
      loading: false,
      data: [],
      pagination: {
        showQuickJumper: true,
        showSizeChanger: true,
        defaultCurrent: 1, // 默认当前页数
        defaultPageSize: 10, // 默认当前页显示数据的大小
        total: 0, // 总数，必须先有
        pageSizeOptions: ['10', '20', '30', '50', '100'],
        showTotal: (total, range) => {
          return (
            <div>
              共 <span>{total}</span> 条
            </div>
          )
        },
        onShowSizeChange: (current, pageSize) => {
          this.pageSize = pageSize
        }
      },
      queryParam: {}
    }
  },
  created() {
    // 获取列表
    this.fetch()
  },
  methods: {
    // 改变每页数量时更新显示
    onChange(pagination) {
      this.pagination.defaultCurrent = pagination.current
      this.pagination.defaultPageSize = pagination.pageSize
      this.fetch()
    },
    // 搜索
    searchBtn() {
      this.pagination.defaultCurrent = 1
      this.fetch()
    },
    // 动态获取列表
    fetch() {
      this.loading = true
      const params = {
        page: this.pagination.defaultCurrent,
        pagesize: this.pagination.defaultPageSize
      }
      this.$common
        .handleGet('/users/answerlists/' + this.$route.params.id, Object.assign({}, params, this.queryParam))
        .then((res) => {
          this.loading = false
          if (res.code === 200) {
            const result = res.data
            const pagination = { ...this.pagination }
            pagination.total = result.totalInfo.total
            this.data = result.lists
            this.pagination = pagination
          }
        })
        .catch(() => {
          this.$notification.error({
            message: '错误',
            description: '请求信息失败，请重试'
          })
        })
    },
    // 删除某一个
    deleteOne(id) {
      this.handleDelete(id)
    },
    handleDelete(deleteId) {
      this.$common
        .deleteOne('/user/delete/' + deleteId, {}, deleteId)
        .then((res) => {
          if (res) {
            this.fetch()
          }
        })
        .catch(() => {
          this.$notification.error({
            message: '错误',
            description: '请求信息失败，请重试'
          })
        })
    }
  }
}
</script>
